﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityWebSocket.Demo;
using DG.Tweening;

public partial class UILogin : UIBase
{
    public Button buttonLogin;
    private Sequence _sequenceButton;
    public void onButtonLoginClick()
    {
        if (!IsFillAccountOrPsw())
            return;

        string ip = inputIp.text;
        int port;
        if (!int.TryParse(inputPort.text, out port))
        {
            Debug.LogError("can not parse port:" + port);
        }
        if (WebSocketClient.Instance.state == UnityWebSocket.WebSocketState.Open)
        {
            //onConnect2Login();
            //return;
            WebSocketClient.Instance.Close();
        }

        //隐藏按钮
        _sequenceButton = DOTween.Sequence(buttonLogin);
        _sequenceButton.Append(buttonLogin.transform.DOScaleX(0, 0.2f));
        _sequenceButton.Append(buttonLogin.transform.DOScaleX(1, 1f));

        WebSocketClient.Instance.Connect("ws://" + ip + ":" + port, onConnect2Login);
    }

    private void onConnect2Login()
    {
        //还原按钮
        _sequenceButton?.Kill();
        buttonLogin.transform.localScale = Vector3.one;

        string account = this.inputAccount.text.Trim();
        string psw = this.inputPassword.text.Trim();
        if (string.IsNullOrEmpty(account))
        {
            Debug.LogError("account = null");
            return;
        }
        if (string.IsNullOrEmpty(psw))
        {
            Debug.LogError("psw = null");
            return;
        }
        var req = new login();
        req.user = account;
        req.psw = psw;
        req.versioncode = CommonDefine.VersionCode;
        WebSocketClient.Instance.SendSerialize(buttonLogin, req);

        DataPlayer.Instance.cacheUser = account;
        DataPlayer.Instance.cachePsw = psw;
    }

    private void OnHandleLoginRsp(System.Object data)
    {
        var rsp = data as login_rsp;
        switch (rsp.ret)
        {
            case NetErrorCode.PswError:
                UIDialog.Instance.Show(XLocalization.Get("提示"), XLocalization.Get("密码错误，请重输密码。"), null, null);
                break;
            case NetErrorCode.UserNotFind:
                UIDialog.Instance.Show(XLocalization.Get("提示"), XLocalization.Get("账户不存在，请问要注册此账户吗？"), () => {
                    onButtonRegistClick();
                }, null);
                break;
            case NetErrorCode.None:
                this.CloseUI();
                DataPlayer.Instance.userId = rsp.reason;
                DataPlayer.Instance.cacheGroupId = rsp.groupid;
                SceneMgr.Instance.LoadScene(SceneName.SceneMain);
                UITips.Instance.Show(XLocalization.Get("登录成功"));
                break;
            case NetErrorCode.VersionOld:
                UIDialog.Instance.Show(XLocalization.Get("提示"), XLocalization.Get("版本过老，请点击“确定”下载最新版本\n" + rsp.reason), () => {
                    Application.OpenURL(rsp.reason);
                }, null);
                break;
            default:
                Debug.LogError("unhandle error:" + rsp.ret);
                break;
        }
    }
}
